<template>
    <div>
        <div>
            <el-input 
                    size="small"
                    placeholder="添加职位..."
                    suffix-icon="el-icon-plus"
                    @keydown.enter.native="addPosition"
                    v-model="pos.name"
                    style="width: 300px; margin-right: 8px;">
            </el-input>
            <el-button size="small" icon="el-icon-plus" type="primary" @click="addPosition">添加职位</el-button>
        </div>
        <div class="posManaMain">
            <el-table
                    size="small"
                    stripe
                    border
                    :data="positions"
                    @selection-change="handleSelectionChange"
                    style="width: 70%">
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column prop="id" label="编号" width="55"></el-table-column>
                <el-table-column prop="name" label="职位" width="120"></el-table-column>
                <el-table-column prop="createDate" label="创建时间" width="200"></el-table-column>
                <el-table-column prop="enabled" label="是否启用" width="80">
                    <template slot-scope="scope">
                        <!--JSON.parse(scope.row.enabled)  将字符串转为boolean值-->
                        <el-tag v-if="JSON.parse(scope.row.enabled)" type="success">已启用</el-tag>
                        <el-tag v-else type="danger">未启用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="编号">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="showEditView(scope.$index,scope.row)">编辑
                        </el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.$index,scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-button type="danger" size="small" :disabled="this.multipleSelection.length === 0" @click="deleteMany">批量删除
        </el-button>
        <el-dialog 
                title="编辑职位"
                :visible.sync="dialogVisible"
                width="30%">
            <div>
                <div style="margin-bottom:5px;margin-left:8px">
                    <el-tag>职位名称</el-tag>
                </div>
                <div style="margin-bottom:5px">
                    <el-input v-model="updatePos.name" size="small" class="updatePosInput"></el-input>
                </div>
                <div style="margin-left:8px">
                    <el-tag>是否启用</el-tag>
                    <el-switch v-model="updatePos.enabled"
                                    active-color="#13ce66"
                                    inactive-color="#ff4949"
                                    active-text="启用"
                                    inactive-text="禁用"
                                    style="margin-left: 8px;">
                    </el-switch>
                </div>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button size="small" @click="dislogVisible = false">取 消</el-button>
                <el-button size="small" type="primary" @click="doUpdate">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name:"PosMana",
    data(){
        return{
            //添加信息
            pos:{
                name: '',
            },
            // 所有信息
            positions: [],
            // 编辑框
            dialogVisible: false,
            // 更新信息
            updatePos:{
                name: '',
                enabled: '',
            },
            // 多选
            multipleSelection: []
        }
    },

    //周期函数
    mounted() {
        this.initPositions();
    },

    methods: {
        
        //编辑职位信息函数
        doUpdate(){
            this.putRequest('/system/basic/pos/',this.updatePos).then(resp => {
                if (resp){
                    this.initPositions();
                    this.dialogVisible = false;
                }
            })
        },

        //添加职位函数
        addPosition(){
            if (this.pos.name) {
                this.postRequest('/system/basic/pos/',this.pos).then(resp =>{
                    if (resp){
                        this.initPositions();
                        this.pos.name = '';
                    }
                })
            } else {
                this.$message.error('职称名字不能为空!');
            }
        },

        //初始化数据函数（重新获取数据）
        initPositions(){
            this.getRequest('/system/basic/pos/').then(resp => {
                if (resp){
                    this.positions = resp;
                }
            })
        },

        //删除职位函数
        handleDelete(index,data){
            this.$confirm('此操作将永久删除['+data.name+']职位，是否继续？','提示',{
                confirmButtonText:'确定',
                cancelButtonText:'取消',
                type :'warning'
            }).then(() => {
                this.deleteRequest('/system/basic/pos/'+data.id).then(resp =>{
                    if (resp){
                        this.initPositions();
                    }
                })
            }).catch(() => {
                this.$message({
                    type: "info",
                    message: "已取消操作"
                })
            })
        },

        // 点击编辑展示信息的函数
        showEditView(index,data){
            Object.assign(this.updatePos,data);
            this.updatePos.createDate = '';
            this.dialogVisible = true;
        },

        //多选
        handleSelectionChange(rows){
            // 将选择的值赋值给multipleSelection
            this.multipleSelection = rows;
        },

        // 批量删除
        deleteMany(){
            this.$confirm('此操作将永久删除['+this.multipleSelection.length+']个职位，是否继续？','提示',{
                confirmButtonText:'确定',
                cancelButtonText:'取消',
                type :'warning'
            }).then(() => {
                let ids = '?';
                    this.multipleSelection.forEach(item => {
                        ids += 'ids=' + item.id + '&';
                })
                this.deleteRequest('/system/basic/pos'+ ids).then(resp =>{
                    if (resp){
                        this.initPositions();
                    }
                })
            }).catch(() => {
                this.$message({
                    type: "info",
                    message: "已取消操作"
                })
            })
        }
    }

}
</script>

<style>
.posManaMain {
    margin-top: 10px;
    margin-bottom: 10px;
}

.updatePosInput {
    width: 200px;
    margin-left: 8px;
}
</style>